package kr.hkit.summary_java.erp.UI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JButton;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summary_java.erp.dao.DaoInterface;
import kr.hkit.summary_java.erp.dto.CodeTitle;

import java.awt.BorderLayout;

import javax.swing.BoxLayout;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.SQLException;

public class PanelTitle extends JPanel implements ActionListener {
	private InputPanel panel_titleno;
	private JPanel panel_btn;
	private InputPanel panel_titlename;
	private JButton btnOk;
	private CodeTitle d;

	private DaoInterface<CodeTitle> dao;
	/**
	 * Create the panel.
	 */
	public PanelTitle(DaoInterface<CodeTitle> dao) {
		this.dao = dao;
		initialize();
	}
	private void initialize() {
		setLayout(new GridLayout(0, 1, 0, 0));
		
		panel_titleno = new InputPanel("직책번호");
		add(panel_titleno);
		panel_titleno.getTf().addFocusListener(new FocusAdapter() {

			@Override
			public void focusLost(FocusEvent e) {
				super.focusLost(e);
				int no = Integer
						.parseInt(panel_titleno.getTf().getText().trim());
				d = new CodeTitle();
				d.setTitleNo(no);
				try {
					d = dao.searchItem(d);
					panel_titlename.setTf(d.getTitleName());
					if (!btnOk.getText().equals("검색")) {
						panelEnable();
					}
				} catch (SQLException e1) {
					e1.printStackTrace();
				} catch (NullPointerException e1) {
					if (!btnOk.getText().equals("추가"))
						JOptionPane.showMessageDialog(PanelTitle.this,
								"해당 직책이 없습니다.");
					else
						JOptionPane.showMessageDialog(PanelTitle.this, "추가 가능");
				}
			}

		});
		
		panel_titlename = new InputPanel("직책명");
		add(panel_titlename);
		
		panel_btn = new JPanel();
		add(panel_btn);
		panel_btn.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnOk = new JButton();
		btnOk.addActionListener(this);
		panel_btn.add(btnOk);
	}

	private CodeTitle getDept() {
		int dno = Integer.parseInt(panel_titleno.getTf().getText().trim());
		String name = panel_titlename.getTf().getText();
		return new CodeTitle(dno, name);
	}

	private void panelDisable() {
		panel_titleno.getTf().setEnabled(true);
		panel_titlename.getTf().setEnabled(false);
	}

	private void panelEnable() {
		panel_titleno.getTf().setEnabled(false);
		panel_titlename.getTf().setEnabled(true);
	}

	private void panelClear() {
		panel_titleno.setTf("");
		panel_titlename.setTf("");
	}

	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("추가")) {
			actionPerformedBtnAdd(e);
		} else if(e.getActionCommand().equals("삭제")){
			actionPerformedBtnDel(e);
		} else if(e.getActionCommand().equals("수정")){
			actionPerformedBtnUpdate(e);
		} else if(e.getActionCommand().equals("검색")){
			actionPerformedBtnSearch(e);
		} else {//리스트보기
			actionPerformedBtnList(e);
		}
	}
	private void actionPerformedBtnList(ActionEvent e) {
	}
	private void actionPerformedBtnSearch(ActionEvent e) {		
	}
	private void actionPerformedBtnUpdate(ActionEvent e) {		
	}
	private void actionPerformedBtnDel(ActionEvent e) {
	}
	private void actionPerformedBtnAdd(ActionEvent e) {
	}
	
	public void setBtnOk(String btnOk) {
		this.btnOk.setText(btnOk);
	}
}
